<?php

define('ROOTPATH',str_replace('\libs','',dirname(__FILE__)));


class Wtemplate{
	
	private $_tags;
	private $_styles;
	public $template_dir;
	private $_components;
	
	function Wtemplate(){
		$this->_styles = array();
		$this->_tags = array();
		$this->_components = array();
		
		$this->template_dir = ROOTPATH ."/template";
	}
	function component($path,$name){
		
		$this->_tags[$name] = array(
			"path"=>$this->template_dir .'/'.$path,
			"params"=>array()
		);
	}


	function display($path,$vars=array()){
		
		
		$dir = $this->template_dir;
		
		
		$template = $this->compile_template($dir.'/'.$path,$vars);
		
		$result = $this->build($template,$vars);
		
		
		$styles = array();
		foreach($this->_styles as $val){
			$styles[] = $val;
		}
		
		$result .= PHP_EOL ."<style>". PHP_EOL .implode(PHP_EOL,$styles).PHP_EOL ."</style>";
		
		echo $result;
	}

	
	function compile_template($_path,$_params=array(),$name='page'){
		
		if(!file_exists($_path)){
			echo $_path.'路径模板不存在';
			//return '';
		}
		
		$_arr = array();
		$_farr = array('id','class','style','title','dir','lang','width','height','contextmenu','contenteditable','draggable','dropzone','hidden','spellcheck','tabindex','translate',
						'name','placeholder','value','type','checked','selected',
						'href','src',
						'onclick','onmousedown','onmouseup','onmousemove');
		foreach($_params as $key=>$val){
			if(in_array($key,$_farr)){
				$_arr[] = "{$key}=\"{$val}\"";
			}
		}
		$attrs = implode(' ',$_arr);
		$id = $this->get_id();
		
		$_component = $this->get_component($id,$name,$_params);
		$this->_components[] = $_component;
		
		extract($_params);
		
		ob_start(); // 开始输入缓冲
		@include($_path);
		
		$tt = ob_get_contents ();
		ob_end_clean();//输出全部内容到浏览器 
		
		
		//style处理
		$tt = $this->style_compile($tt);
		
		return $tt;
	}
	
	//样式编译
	function style_compile($tt){
		//style处理
		$reg = "/<style([^>]*?)>([\s\S]*?)<\/style>/is";
		preg_match($reg,$tt,$matchs);
		$tt = preg_replace($reg,"",$tt);
		if(count($matchs)>0){
			$rnd = 'data-v-'.rand(10000,99999);
			
			$style = $matchs[2];
			$styles = $this->style_cl($style);
			
			if(trim($matchs[1])=='scoped'){
				$tt = preg_replace("/<(div|a|ul|li|ol|span|em|img|table|tr|td|th|dl|dd|dt|h1|h2|h3|h4|h5|h6|input|label)([^>]*?)>/is","<$1 {$rnd} $2>",$tt);
				foreach($styles as $key=>$val){
					$styles[$key] = preg_replace("/(^.*?)([\{|\:])/i","\n$1[{$rnd}]$2",$val);
				}
			}
			
			$this->_styles[$rnd] = implode(PHP_EOL,$styles);
		}
		return $tt;
	}
	//样色处理
	function style_cl($style){
		$fg = explode('}',$style);
		$ls_style = array();
		
		foreach($fg as $val){
			if(empty($val)){
				continue;
			}
			$css = $val . "}";
			$css = preg_replace("/\n|  /is",'',$css);
			$css = trim($css);
			
			$ls_style[] = $css;
		}
		
		return $ls_style;
	}
	
	function build($template,$vars=array()){
		
		
		foreach($this->_tags as $key=>$val){
			//$reg = "/<{$key}([^>]*?)>([\s\S]*?)<\/{$key}>/is";
			
			//'#\{(Cms:[a-z]+)[^\}]*\}(([^\{\}]|{(?!=Cms)[^\}]*\}|(?R))*)\{\/\\1\}#i'
			$reg  = "/<({$key})([^>]*?)>(([^<>]|<(?!={$key})[^>]*>|(?R))*?)<\/{$key}>/is";
			
			
			$template = preg_replace_callback($reg,function($matchs)  use ($val,$vars) {
				//var_dump($matchs);
				$tagname = $matchs[1];
				$param = $matchs[2];
				$content = $matchs[3];
				$params = array();
				
				if(!empty($param)){
					
					$ls_params = $this->getparam($param,$vars);
					
					if($ls_params && is_array($ls_params)){
						$params = $ls_params;
					}
				}
				
				$params['slot'] = $content;
				
				$params = array_merge($params,$val['params']);
				
				
				
				$result = $this->compile_template($val['path'],$params,$tagname);
				
				return $result;
			},$template);

			
		}

		
		
		
		return $template;
	}
	
	
	function getparam($param,$vars=array()){
		
		
		preg_match_all("/ [\s\S]*?\=[\'\"][\s\S]*?[\'\"]/is",$param,$ls_matchs);
		if(count($ls_matchs)<1){
			return false;
		}
		$fg = array();
		
		foreach($ls_matchs[0] as $val){
			$fg[] = $val;
		}
		$arr = array();
		foreach($fg as $val){
			if(empty($val)){
				continue;
			}
			$fg1 = explode('=',$val);
			
			if(count($fg1)<2){
				continue;
			}
			$ls_val = $fg1[1];
			$ls_val = preg_replace('/^\"|^\'|\"$|\'$/is','',$ls_val);
			
			
			if(preg_match('/\{\$(.*?)\}/is',$ls_val,$matchs)){
				$kk  = trim($matchs[1]);
				
				
				if(isset($vars[$kk])){
					$ls_val = $vars[$kk];
				}	
			}
			$ls_key = trim($fg1[0]);
			$arr[$ls_key] = $ls_val;
		}

		return $arr;
		
	}
	

	
	function get_component($id,$name,$params){
		$arr = array(
			"id"=>$id,
			"name"=>$name,
			"params"=>$params
		);
		
		return $arr;
	}
	
	function get_id(){
		return count($this->_components)+1;
	}
	
	function id_get_component($id){
		
		foreach($this->_components as $val){
			if($val['id']==$id){
				return $val;
			}
		}
		return false;
	}
}